<template>
  <div class="case-display-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <h1 class="page-title">优秀案例展示</h1>
      <p class="page-subtitle">探索创新解决方案与成功实践</p>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-section">
      <div class="search-wrapper">
        <el-input 
          v-model="searchQuery" 
          placeholder="请输入关键词搜索案例..." 
          clearable
          size="large"
          class="search-input"
        >
          <template #prefix>
            <i class="el-icon-search prefix-icon"></i>
          </template>
        </el-input>
        <el-button 
          type="primary" 
          @click="searchCases"
          class="search-button"
        >
          搜索案例
        </el-button>
      </div>
    </div>
    
    <!-- 筛选和排序 -->
    <div class="filter-section">
      <div class="filter-wrapper">
        <el-radio-group v-model="sortType" size="small">
          <el-radio-button label="latest">最新发布</el-radio-button>
          <el-radio-button label="name">名称排序</el-radio-button>
        </el-radio-group>
        <div class="result-count">
          共 <strong>{{ filteredCases.length }}</strong> 个案例
        </div>
      </div>
    </div>
    
    <!-- 案例列表区域 -->
    <div class="case-grid">
      <el-card 
        v-for="(caseItem, index) in sortedCases" 
        :key="index" 
        class="case-card"
        shadow="hover"
        @click="goToDetails(caseItem)"
      >
        <div class="card-badge" v-if="index < 3">推荐</div>
        <template #header>
          <div class="card-header">
            <img :src="caseItem.img" alt="案例图片" class="case-image">
            <div class="case-overlay">
              <div class="overlay-content">
                <el-button 
                  type="text" 
                  icon="el-icon-view" 
                  class="quick-view-btn"
                >
                  快速预览
                </el-button>
              </div>
            </div>
          </div>
        </template>
        <div class="case-info">
          <div class="case-title">{{ caseItem.title }}</div>
          <div class="case-tags">
            <el-tag size="mini" v-for="tag in getTags(caseItem.content)" :key="tag">{{ tag }}</el-tag>
          </div>
          <div class="case-content">{{ caseItem.content }}</div>
        </div>
        <div class="case-footer">
          <div class="case-meta">
            <span class="create-time"><i class="el-icon-date"></i> {{ caseItem.create_time }}</span>
            <span class="view-count"><i class="el-icon-view"></i> 248 次浏览</span>
          </div>
          <el-button 
            type="primary" 
            size="small"
            class="view-details-btn"
          >
            查看详情
          </el-button>
        </div>
      </el-card>
    </div>
    
    <!-- 无结果提示 -->
    <div v-if="showNoResult" class="no-result-message">
      <el-empty 
        description="未找到匹配的案例"
        image="empty"
      >
        <template #bottom>
          <el-button 
            type="primary" 
            @click="resetSearch"
          >
            清除搜索条件
          </el-button>
        </template>
      </el-empty>
    </div>
    
    <!-- 分页控件 -->
    <div class="pagination-section" v-if="!showNoResult">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="filteredCases.length"
        :page-size="8"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage, ElCard, ElInput, ElButton, ElEmpty, ElPagination, ElRadioGroup, ElRadioButton, ElTag } from 'element-plus'
import { useRouter } from 'vue-router'

// 导入案例图片
import caseImg1 from '@/assets/t1.jpg'
import caseImg2 from '@/assets/t2.jpg'
import caseImg3 from '@/assets/t3.jpg'
import caseImg4 from '@/assets/t4.jpg'

const router = useRouter()

// 搜索和筛选相关数据
const searchQuery = ref('')
const sortType = ref('latest')
const currentPage = ref(1)
const pageSize = ref(8)

// 案例数据
const originalCases = reactive([
  {
    title: '优秀案例1',
    img: caseImg1,
    create_time: '2024-10-23',
    content: '康院优秀案例收集与分享平台（学生端）',
    tags: ['学生平台', '数据收集', '分享系统']
  },
  {
    title: '优秀案例2',
    img: caseImg2,
    create_time: '2024-10-22',
    content: '基于vue及可视化技术的阿尔兹海默症可视化大屏',
    tags: ['数据可视化', '医疗健康', '大屏展示']
  },
  {
    title: '优秀案例3',
    img: caseImg3,
    create_time: '2024-10-21',
    content: '基于VUE的学生个人画像网',
    tags: ['学生画像', '数据分析', '教育技术']
  },
  {
    title: '优秀案例4',
    img: caseImg4,
    create_time: '2024-10-20',
    content: '康院优秀案例收集与分享平台（教师端）',
    tags: ['教师平台', '案例管理', '教育资源']
  }
])

// 计算属性：过滤后的案例列表
const filteredCases = computed(() => {
  if (!searchQuery.value.trim()) {
    return originalCases
  }
  
  const query = searchQuery.value.trim().toLowerCase()
  return originalCases.filter(caseItem => 
    caseItem.title.toLowerCase().includes(query) || 
    caseItem.content.toLowerCase().includes(query)
  )
})

// 计算属性：排序后的案例列表
const sortedCases = computed(() => {
  const cases = [...filteredCases.value]
  
  if (sortType.value === 'latest') {
    return cases.sort((a, b) => new Date(b.create_time) - new Date(a.create_time))
  } else if (sortType.value === 'name') {
    return cases.sort((a, b) => a.title.localeCompare(b.title))
  }
  
  return cases
})

// 计算属性：当前页显示的案例
const paginatedCases = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return sortedCases.value.slice(start, end)
})

// 是否显示无结果提示
const showNoResult = computed(() => 
  searchQuery.value.trim() && filteredCases.value.length === 0
)

// 从内容中提取标签
const getTags = (content) => {
  const tags = []
  if (content.includes('学生')) tags.push('学生')
  if (content.includes('平台')) tags.push('平台')
  if (content.includes('数据')) tags.push('数据')
  if (content.includes('可视化')) tags.push('可视化')
  if (content.includes('教师')) tags.push('教师')
  return tags
}

// 搜索功能
const searchCases = () => {
  if (searchQuery.value.trim()) {
    currentPage.value = 1 // 重置页码
  } else {
    resetSearch()
  }
}

// 重置搜索
const resetSearch = () => {
  searchQuery.value = ''
  currentPage.value = 1
}

// 跳转到详情页
const goToDetails = (caseItem) => {
  if (caseItem.title === '优秀案例1') {
    router.push('/error')
  } else {
    router.push({
      path: '/Details',
      query: caseItem
    })
  }
}

// 分页相关方法
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
}

// 页面加载时的初始化
onMounted(() => {
  // 添加页面进入动画
  const container = document.querySelector('.case-display-container')
  if (container) {
    container.classList.add('fade-in')
  }
})
</script>

<style scoped>
/* 基础样式 */
.case-display-container {
  max-width: 1360px;
  margin: 0 auto;
  padding: 30px 20px;
  opacity: 0;
  animation: fadeIn 0.5s ease-in-out forwards;
}

/* 页面头部样式 */
.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 16px;
  color: #606266;
}

/* 搜索区域样式 */
.search-section {
  padding: 25px 0;
  background-color: #f9fafc;
  border-radius: 16px;
  margin-bottom: 30px;
}

.search-wrapper {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  gap: 15px;
}

.search-input {
  flex: 1;
  border-radius: 30px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.prefix-icon {
  color: #909399;
  margin-left: 15px;
}

.search-button {
  border-radius: 30px;
  background-color: #279c55;
  border-color: #279c55;
  transition: all 0.3s;
  padding: 0 30px;
  font-weight: 500;
}

.search-button:hover {
  background-color: #1e7d44;
  border-color: #1e7d44;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(39, 156, 85, 0.2);
}

/* 筛选区域样式 */
.filter-section {
  margin-bottom: 25px;
}

.filter-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-count {
  color: #606266;
  font-size: 14px;
}

/* 案例卡片样式 */
.case-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 28px;
}

.case-card {
  border-radius: 16px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  border: none;
  position: relative;
}

.case-card:hover {
  transform: translateY(-6px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.card-badge {
  position: absolute;
  top: 15px;
  left: 15px;
  background-color: #ff7d00;
  color: white;
  font-size: 12px;
  font-weight: 500;
  padding: 3px 8px;
  border-radius: 4px;
  z-index: 10;
}

.card-header {
  padding: 0;
  height: 220px;
  overflow: hidden;
  position: relative;
}

.case-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.case-card:hover .case-image {
  transform: scale(1.08);
}

.case-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  opacity: 0;
  transition: opacity 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
}

.case-card:hover .case-overlay {
  opacity: 1;
}

.quick-view-btn {
  color: white;
  font-size: 14px;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s;
}

.quick-view-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

.case-info {
  padding: 20px;
}

.case-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.case-tags {
  margin-bottom: 15px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.case-tags .el-tag {
  background-color: #f2f3f5;
  color: #606266;
  border: none;
}

.case-content {
  font-size: 14px;
  color: #606266;
  line-height: 1.7;
  max-height: 68px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  margin-bottom: 20px;
}

.case-footer {
  padding: 0 20px 20px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.case-meta {
  display: flex;
  gap: 15px;
  color: #909399;
  font-size: 13px;
}

.case-meta span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.view-details-btn {
  background-color: #279c55;
  border-color: #279c55;
  color: white;
  border-radius: 4px;
  padding: 6px 15px;
  transition: all 0.3s;
}

.view-details-btn:hover {
  background-color: #1e7d44;
  border-color: #1e7d44;
  transform: translateY(-1px);
}

/* 无结果提示样式 */
.no-result-message {
  padding: 80px 0;
}

/* 分页控件样式 */
.pagination-section {
  margin-top: 40px;
  display: flex;
  justify-content: center;
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>  